<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>后台管理</title>
<link href="css/bundled.css" rel="stylesheet" media="screen">
<link rel="stylesheet" type="text/css" href="css/common.css" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<!-- <script type="text/javascript" src="js/libs/modernizr.min.js"></script> -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<link href="css/jquery-confirm.css" rel="stylesheet" media="screen">

<script src="js/jquery-confirm.js" type="text/javascript"></script>
</head>
<body >
	<div class="container clearfix">

		<!--/sidebar-->
		<div class="main-wrap">

			<div class="crumb-wrap">
				<div class="crumb-list">
					<span class="crumb-name">通知管理</span>
				</div>
			</div>
			<div class="search-wrap">
				<div class="search-content">
					<form  οnsubmit="return false">
						<table class="search-tab">
							<tr>
								<th width="120">选择分类:</th>
								<td><select name="search-sort" id="searchType">
										<option value="0">全部</option>
										<option value="1">已截至</option>
										<option value="2">未截止</option>
								</select></td>
								<th width="70">关键字:</th>
								 <input type="text" id="" name="" style="display: none;"/> 
								<td><input class="" placeholder="发布者/发布对象/通知标题"
									name="keywords" value="" id="searchText" type="text"></td>
								<td><input type="button" class="btn btn-primary btn2" id="btnSearch"
									value="查询" ></input></td>
							</tr>
						</table>
					</form>
				</div>
			</div>
			<div class="result-wrap">
				<form name="myform" id="myform" method="post" οnsubmit="return false;">
					<div class="result-title">
						<div class="result-list">
							<a id="batchDel" href="javascript:void(0)"><img
								src="images/delete.png" style="weight: 15px; height: 15px"></img>批量删除</a>
							
						</div>
					</div>
					<div class="result-content">
						<table class="result-tab" width="100%" id="table">
						<tr>
								<th class="tc" width="5%"><input class="allChoose" name="allChoose"
									type="checkbox" id="chooseAll"></th>
								
								<th>标题</th>
								<th>发布人</th>
								<th>发布对象</th>
								<th>截止时间时间</th>
								<th>提交数</th>
								<th>操作</th>
							</tr>
						</table>
						<div class="list-page">
							共<strong id="pageNum">0</strong>条
						</div>
					</div>
				</form>
			</div>
		</div>
		<!--/main-->
	</div>
</body>
<script type="text/javascript">
$(function() {
	var sType;
	//获取截至还是未截止
	function searchDeadLine(searchText){
		var flag=false;
		if(searchText==undefined){
			flag=true;
		}
		$.ajax({
			url: '/MyWebPlus/getNoticeInformation',
			type: 'get',
			dataType: 'text',
			async :false,
			success: function(data) {
		        var myObject = eval("("+data+")"); 
		        var cnt=0;
		        for(var p in myObject){
		        	if(flag||myObject[p].maker.indexOf(searchText)!=-1||myObject[p].object.indexOf(searchText)!=-1
							 ||myObject[p].title.indexOf(searchText)!=-1){
		        		if(myObject[p].deadLine<getTime()&&sType==1){
			        		$('#table').append("<tr><td class='tc'><input name='idNum' value='"+myObject[p].noticeID+"' type='checkbox'></td>" +
				                    "<td title='"+myObject[p].noticeID+"'><a target='_blank' href='nameList.html?noticeID="+myObject[p].noticeID+"' title='"+myObject[p].noticeID+"'>" + myObject[p].title + "</a></td>" +
				                    "<td>" + myObject[p].maker + "</td>" +
				                    "<td>" + myObject[p].object + "</td>" +
				                    "<td>" + myObject[p].deadLine + "</td>" +
				                    "<td>" +done[p] +"/"+sum[p] + "</td>" +
				                    "<td>" +
				                    "<a class='link-del' title='"+myObject[p].noticeID+"' href='#'>删除 </a></td></tr>");
			        		cnt++;
			        	}else if(myObject[p].deadLine>=getTime()&&sType==2){
			        		$('#table').append("<tr><td class='tc'><input name='idNum' value='"+myObject[p].noticeID+"' type='checkbox'></td>" +
				                    "<td title='"+myObject[p].noticeID+"'><a target='_blank' href='nameList.html?noticeID="+myObject[p].noticeID+"' title='"+myObject[p].noticeID+"'>" + myObject[p].title + "</a></td>" +
				                    "<td>" + myObject[p].maker + "</td>" +
				                    "<td>" + myObject[p].object + "</td>" +
				                    "<td>" + myObject[p].deadLine + "</td>" +
				                    "<td>" +done[p] +"/"+sum[p] + "</td>" +
				                    "<td>" +
				                    "<a class='link-del' title='"+myObject[p].noticeID+"' href='#'>删除 </a></td></tr>");
			        		cnt++;
			        	}
		        	}
		        	$('#pageNum').text(cnt);
		        }
			}
		})
	}
	var isCheckAll = false;
	$('#table').on("click","#chooseAll",function(){
		console.log(isCheckAll)
		if (isCheckAll) {
			$("input[name='idNum']").each(function() {
				this.checked = false;
			});
			isCheckAll = false;
		} else {
			$("input[name='idNum']").each(function() {
				this.checked = true;
			});
			isCheckAll = true;
		}
	})
	//批量删除
	$('#batchDel').on("click",function(){
		var chk_value =[]; 
	    $('input[name="idNum"]:checked').each(function(){ 
	        chk_value.push($(this).val()); 
	    }); 
	    $.confirm({
		    title: '警告!!',
		    content: '确定要删除这'+chk_value.length+'条通知吗',
		    buttons:{
		    	YES: function(){
		    		for(var index in chk_value){
		    	    	console.log(chk_value[index]);
		    	    	deleteNotice(chk_value[index]);
		    	    }
		    		$('#table').empty();
		    		initTable();
		    		search();
					$.alert("删除成功");
			    },
			    NO: function(){
			        
			    }
		    }
		});
	    
	})
	//回车按键响应
	 $('#searchText').keydown(function (event) {
        if (event.keyCode == 13) {
          $('#btnSearch').trigger('click');
        }
      });
	//条件查询
	$('#btnSearch').on("click",function(){
		console.log(111);
		sType=$('#searchType').val();
		var searchText=$('#searchText').val();
		if(sType==0){
			$('#table').empty();
			initTable();
    		search(searchText);
		}else{
			$('#table').empty();
			initTable();
			searchDeadLine(searchText);
		}
		
	});
	//console.log(1);
	//获取完成的和未完成的通知人数
	var done=[];
	var sum=[];
	function findNoticeCount(){
		$.ajax({
			url: '/MyWebPlus/getNoticeInformation',
			type: 'get',
			data:{"noticeNum":1},
			dataType: 'text',
			async :false,
			success: function(data) {
		        var myObject = eval("("+data+")"); 
		       
				for(var p in myObject){
					done[p]=myObject[p].undone;
					sum[p]=myObject[p].sum;
				}
			}
		})
	}

	//获取所有的通知信息
	function search(searchText){
		findNoticeCount();
		var flag=false;
		if(searchText==undefined){
			flag=true;
		}
		
		$.ajax({
			url: '/MyWebPlus/getNoticeInformation',
			type: 'get',
			dataType: 'text',
			success: function(data) {
		        var myObject = eval("("+data+")"); 
		        var cnt=0;
				for(var p in myObject){
					 if(flag||myObject[p].maker.indexOf(searchText)!=-1||myObject[p].object.indexOf(searchText)!=-1
							 ||myObject[p].title.indexOf(searchText)!=-1){
						 cnt++;
						 
						 $('#table').append("<tr><td class='tc'><input name='idNum' value='"+myObject[p].noticeID+"' type='checkbox'></td>" +
				                    "<td title='"+myObject[p].noticeID+"'><a target='_blank' href='nameList.html?noticeID="+myObject[p].noticeID+"' title='"+myObject[p].noticeID+"'>" + myObject[p].title + "</a></td>" +
				                    "<td>" + myObject[p].maker + "</td>" +
				                    "<td>" + myObject[p].object + "</td>" +
				                    "<td>" + myObject[p].deadLine + "</td>" +
				                    "<td>" +done[p] +"/"+sum[p] + "</td>" +
				                    "<td>" +
				                    "<a class='link-del' title='"+myObject[p].noticeID+"' href='#'>删除 </a></td></tr>");
					 }
					 $('#pageNum').text(cnt);
				}
			}
		})
	}
	search();
	
	//删除按钮响应事件
	$('#table').on("click","a",function(){
		if($(this).attr("href")!="#"){
			return ;
		}
		var deletedNoticeID;
		deletedNoticeID=$(this).attr("title");
		deletedNoticeID=decodeURI(deletedNoticeID);
		deletedNoticeID=decodeURI(deletedNoticeID);
		console.log(deletedNoticeID);
		$.confirm({
		    title: '警告!!',
		    content: '确定要删除这条通知吗',
		    buttons:{
		    	YES: function(){
			    		deleteNotice(deletedNoticeID);
			    		$('#table').empty();
			    		initTable();
			    		search();
						$.alert("删除成功");
			    },
			    NO: function(){
			        
			    }
		    }
		});
		   
    });
	//删除通知
	function deleteNotice(deletedNoticeID){
		$.ajax({
			url: '/MyWebPlus/setNoticeInformation',
			type: 'get',
			data:{"deletedNoticeID":deletedNoticeID},
			dataType: 'text',
			async :false,
			success: function(data) {
		       if(data>=0){
		    	   return 1;
		       }
			}
		})
	}
	
	// 设置全选  TODO
	function initTable(){
		 $('#table').append("<tr> <th class='tc' width='5%'><input class='allChoose' name='allChoose' type='checkbox' id='chooseAll'></th>"+
					"<th>标题</th>"+
					"<th>发布人</th>"+
					"<th>发布对象</th>"+
					"<th>截止时间时间</th>"+
					"<th>提交数</th>"+
					"<th>操作</th>"+
				"</tr>");
	}
	function getNow(s) {
	    return s < 10 ? '0' + s: s;
	}

	function getTime(){
		var myDate = new Date();
		//获取当前年
		var year=myDate.getFullYear();
		//获取当前月
		var month=myDate.getMonth()+1;
		//获取当前日
		var date=myDate.getDate(); 
		var h=myDate.getHours();       //获取当前小时数(0-23)
		var m=myDate.getMinutes();     //获取当前分钟数(0-59)
		var s=myDate.getSeconds();  

		var now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);
		return now;
	}
		
	
	
	  
});
</script>
</html>